package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.query.StrategyThemeQuery;
import cn.wolfcode.luowowo.repository.StrategyThemeRepositoey;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.DBHelper;
import cn.wolfcode.luowowo.util.LogicException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class StrategyThemeServiceImpl implements IStrategyThemeService {

    @Autowired
    private StrategyThemeRepositoey strategyThemeRepositoey;

    @Autowired
    private MongoTemplate template;
    private Pageable pageable;

    @Override
    public List<StrategyTheme> list() {
        return strategyThemeRepositoey.findAll();
    }

    @Override
    public StrategyTheme selectOne(String id) {
        return strategyThemeRepositoey.findById(id).orElse(null);
    }

    @Override
    public Page<StrategyTheme> query(StrategyThemeQuery qo) {
        //创建查询对象
        Query query = new Query();
        //页面显示集合
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, StrategyTheme.class, query, pageable);

    }

    @Override
    public void saveOrUpdate(StrategyTheme strategyTheme) {
        if(!StringUtils.hasLength(strategyTheme.getName())){
            throw new LogicException("名字不能为空");
        }
        strategyThemeRepositoey.save(strategyTheme);
    }

    @Override
    public void delete(String id) {
        strategyThemeRepositoey.deleteById(id);
    }


}

